﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FXnaInfrastructure
{

    public class EntityEventArgs : EventArgs
    {

        public EntityEventArgs(FEntity entity)
        {
            this._entity = entity;
 
        }

        FEntity _entity;

        public FEntity Entity
        {
            get { return _entity; }
            set { _entity = value; }
        }
    }




    /// <summary>
    /// 这里我们尝试着做一个fentity类型的集合通用类
    /// 这个集合一般用于不需要绘图表现的fentity实体
    /// </summary>
    [Serializable]
    public class FEntityCollection
    {

        public FEntityCollection()
        {
            this._idGenerator = new IDGenerator(0, "");

        }



        [field:NonSerialized]
        public event EventHandler<EntityEventArgs> EntityAdded;

        [field:NonSerialized]
        public event EventHandler<EntityEventArgs> EntityRemoved;

        [field:NonSerialized]
        public event EventHandler EntityReseted;



        IDGenerator _idGenerator=new IDGenerator ();

        public IDGenerator IdGenerator
        {
            get { return _idGenerator; }

        }



        List<FEntity> _entities = new List<FEntity>();

        public List<FEntity> Entities
        {
            get { return _entities; }
           
        }


        public void Add(FEntity entity)
        {
            this._entities.Add(entity);

            if (this.EntityAdded!=null)
            {
                this.EntityAdded(this,new EntityEventArgs (entity));
                
            }
 

        }



        public void Remove(FEntity entity)
        {
            this._entities.Remove(entity);

            if (this.EntityRemoved!=null)
            {
                this.EntityRemoved(this,new EntityEventArgs(entity));
                
            }
        }



        /// <summary>
        /// 是否存在指定的ID的entity
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsExist(string id)
        {

            foreach (FEntity item in this._entities)
            {
                if (item.Id == id)
                {
                    return true;

                }

            }



            return false;
 

        }



        public FEntity GetEntity(string id)
        {
            foreach (FEntity item in this._entities)
            {
                if (item.Id==id)
                {
                    return item;
                    
                }
                
            }



            return null;

        }


        public void Clear()
        {

            this._entities.Clear();

        }





    }
}
